package com.zbkj.crmeb.chant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.chant.dao.MerchantAddressDao;
import com.zbkj.crmeb.chant.model.MerchantAddress;
import com.zbkj.crmeb.chant.request.EnableTakeAddressRequest;
import com.zbkj.crmeb.chant.request.SaveTakeAddressRequest;
import com.zbkj.crmeb.chant.service.MerchantAddressService;
import com.zbkj.crmeb.enums.AreaTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2021-10-27
 */
@Service
public class MerchantAddressServiceImpl extends ServiceImpl<MerchantAddressDao, MerchantAddress> implements MerchantAddressService {

    @Override
    public List<MerchantAddress> getTakeAddressList(PageParamRequest request, Integer merId,Integer type) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        return list(Wrappers.<MerchantAddress>lambdaQuery().eq(MerchantAddress::getMerId, merId).eq(MerchantAddress::getType,type));
    }

    @Override
    public CommonResult<String> saveTakeAddress(SaveTakeAddressRequest request, Integer merId,Integer type) {
        MerchantAddress merchantAddress = new MerchantAddress();
        BeanUtils.copyProperties(request,merchantAddress);
        merchantAddress.setMerId(merId);
        merchantAddress.setType(type);
        //状态启用
        if(type == 1){
            checkAddressStatus(request.getStatus(), merId, merchantAddress);
        }
        return saveOrUpdate(merchantAddress) ? CommonResult.success() : CommonResult.failed();
    }

    @Override
    public CommonResult<String> enableTakeAddress(EnableTakeAddressRequest request, Integer merId) {
        MerchantAddress merchantAddress = checkAddressExist(request.getId(), merId,1);
        //TODO mod by cdm 2023/06/25 同地区启用状态可以多个，所以去掉下面的检查
        //checkAddressStatus(request.getStatus(), merId, merchantAddress);
        merchantAddress.setStatus(request.getStatus());
        return updateById(merchantAddress) ? CommonResult.success() : CommonResult.failed();
    }

    @Override
    public CommonResult<String> deleteTakeAddress(Integer id, Integer merId,Integer type) {
        checkAddressExist(id, merId,type);
        return removeById(id) ? CommonResult.success() : CommonResult.failed();
    }

    public MerchantAddress checkAddressExist(Integer id, Integer merId,Integer type) {
        MerchantAddress merchantAddress = getOne(Wrappers.<MerchantAddress>lambdaQuery().eq(MerchantAddress::getId,id).eq(MerchantAddress::getMerId,merId).eq(MerchantAddress::getType,type));
        if(null == merchantAddress){
            throw new CrmebException("该地址不存在");
        }
        return merchantAddress;
    }

    public void checkAddressStatus(Integer status, Integer merId, MerchantAddress merchantAddress) {
        if (Objects.equals(status, 1)) {
            int count = count(Wrappers.<MerchantAddress>lambdaQuery().eq(AreaTypeEnum.HK.getType().equals(merchantAddress.getAreaType()),MerchantAddress::getAreaType,AreaTypeEnum.HK.getType())
                    .ne(!AreaTypeEnum.HK.getType().equals(merchantAddress.getAreaType()),MerchantAddress::getAreaType,AreaTypeEnum.HK.getType())
                    .eq(MerchantAddress::getStatus, 1)
                    .eq(MerchantAddress::getMerId, merId)
                    .ne(null != merchantAddress.getId(), MerchantAddress::getId, merchantAddress.getId()));
            if (count > 0) {
                throw new CrmebException("当前区域已有地址启用");
            }
        }
    }
}
